//
//  PersonalViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/4.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "PersonalViewController.h"
#import "LZFoldButton.h"
#import "ModifyLoginPasswordViewController.h"
#import "SetUpMoneyPasswordViewController.h"
#import "SecurityViewController.h"
#import "CreditNameViewController.h"
#import "CardNumberBindingViewController.h"
#import "SignViewController.h"

#import "AFNetworking.h"
@interface PersonalViewController ()<LZFoldButtonDelegate>
{
    LZFoldButton *lz;
}


@end

@implementation PersonalViewController

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"User"];
    [parametersDic setObject:@"SafetyExit" forKey:@"Action"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        if ([[dic objectForKey:@"msg"] isEqualToString:@"退出成功."]) {
//            SignViewController *signVC= [[SignViewController alloc] init];
//            [self.navigationController pushViewController:signVC animated:YES];
//        }else{
            UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alerView addAction:action];
            [self presentViewController:alerView animated:YES completion:nil];
//        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];

    self.title = @"个人设置";
    
    self.personaltitles1 = @[@"修改登录密码",@"设置资金密码",@"安全问题"];


    personaltableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    personaltableView.dataSource = self;
    personaltableView.delegate = self;
//    personaltableView.scrollEnabled = NO;
    personaltableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:personaltableView];
    
    [self personalLZFoldButton];
}

-(void)personalLZFoldButton
{
    NSArray *arr = @[@"元",@"角",@"分",@"厘"];
    
    lz = [[LZFoldButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-50-10, 45*6+60+(45-30)/2, 50, 30) dataArray:arr];
    lz.lzDelegate = self;
    lz.lzFontSize = 12;
    lz.lzHeight = 90;
    lz.lzButtonType = 1;
    if (![[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"]) {
        [lz LZSetTitle:@"元" forState:UIControlStateNormal];
    }
    else{
    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] > 0.2) {
        [lz LZSetTitle:@"元" forState:UIControlStateNormal];
    }
    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] < 0.9 && [[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] > 0.02) {
        [lz LZSetTitle:@"角" forState:UIControlStateNormal];
    }
    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] < 0.09&& [[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] > 0.002) {
        [lz LZSetTitle:@"分" forState:UIControlStateNormal];
    }
    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"yjfl"] < 0.009) {
        [lz LZSetTitle:@"厘" forState:UIControlStateNormal];
    }
    }

//    [lz LZSetTitle:@"元" forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateSelected];
    [lz LZSetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [personaltableView addSubview:lz];
    
    
}

-(void)LZFoldButton:(LZFoldButton*)foldButton didSelectObject:(id)obj {
//    NSLog(@"%@",obj);
    if ([obj isEqualToString:@"元"]) {
        [[NSUserDefaults standardUserDefaults] setFloat:1.0 forKey:@"yjfl"];
    }
    if ([obj isEqualToString:@"角"]) {
        [[NSUserDefaults standardUserDefaults] setFloat:0.1 forKey:@"yjfl"];
    }
    if ([obj isEqualToString:@"分"]) {
        [[NSUserDefaults standardUserDefaults] setFloat:0.01 forKey:@"yjfl"];
    }
    if ([obj isEqualToString:@"厘"]) {
        [[NSUserDefaults standardUserDefaults] setFloat:0.001 forKey:@"yjfl"];
    }
}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }else if (section == 2){
        return 2;
    }else{
    return 1;
    }
  }

//设置表视图中的section（分组）数,默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 5;
}

// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section < 4) {
      return 20;
    }else{
    return 60;
    }
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 45;
}



//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myCell = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.textColor = [UIColor grayColor];

        // 给单元格创建ImageView
        UIImageView *PersonalDetailsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH-15-10, (45-15)/2, 10, 15)];
         PersonalDetailsImageView.tag = 100;
        [cell.contentView addSubview:PersonalDetailsImageView];
        
        UISwitch *personalDetailsSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-50-10, (45-30)/2, 50, 30)];
        personalDetailsSwitch.tag = 101;
        [cell.contentView addSubview:personalDetailsSwitch];
        
        UILabel *personalDetailsLabel = [[UILabel alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH - 100 - 10, (45-20)/2, 100, 20)];
        personalDetailsLabel.tag = 102;
        personalDetailsLabel.textColor = [UIColor grayColor];
        personalDetailsLabel.font = [UIFont systemFontOfSize:14];
        [cell.contentView addSubview:personalDetailsLabel];
    }
    
    UIImageView *PersonalDetailsImageView = (UIImageView *)[cell.contentView viewWithTag:100];
    UISwitch *personalDetailsSwitch = (UISwitch *)[cell.contentView viewWithTag:101];
    UILabel *personalDetailsLabel = (UILabel *)[cell.contentView viewWithTag:102];

    
    //设置数据
    if (indexPath.section == 0) {
        cell.textLabel.text = _personaltitles1[indexPath.row];
        [PersonalDetailsImageView setImage:[UIImage imageNamed:@"icon_arrow"]];
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.hidden = YES;
    }else if(indexPath.section == 1 & indexPath.row == 0){
        cell.textLabel.text = @"绑定卡号姓名";
        [PersonalDetailsImageView setImage:[UIImage imageNamed:@"icon_arrow"]];
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.hidden = YES;
    }else if(indexPath.section == 1 & indexPath.row == 1){
        cell.textLabel.text = @"卡号绑定";
        [PersonalDetailsImageView setImage:[UIImage imageNamed:@"icon_arrow"]];
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.hidden = YES;

    }else if (indexPath.section == 2 & indexPath.row == 0){
        cell.textLabel.text = @"通知设置";
        personalDetailsSwitch.hidden = NO;
        personalDetailsLabel.hidden = YES;
        PersonalDetailsImageView.hidden = YES;
    }else if (indexPath.section == 2 & indexPath.row == 1){
        cell.textLabel.text = @"元角分厘模式";
        PersonalDetailsImageView.hidden = YES;
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.hidden = YES;
//        [cell.contentView addSubview:lz];
    }else if (indexPath.section == 3){
        cell.textLabel.text = @"版本更新";
         PersonalDetailsImageView.hidden = YES;
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.text = @"当前版本1.0";
    }else{
        cell.textLabel.text = @"退出登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
         PersonalDetailsImageView.hidden = YES;
        personalDetailsSwitch.hidden = YES;
        personalDetailsLabel.hidden = YES;
    }
    [personaltableView addSubview:lz];

    return cell;
    
}

//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0 & indexPath.row == 0){
     
    ModifyLoginPasswordViewController *mLPVC= [[ModifyLoginPasswordViewController alloc] init];
    [self.navigationController pushViewController:mLPVC animated:YES];
        
    }else if (indexPath.section == 0 & indexPath.row == 1){
        
    SetUpMoneyPasswordViewController *sUMPVC= [[SetUpMoneyPasswordViewController alloc] init];
    [self.navigationController pushViewController:sUMPVC animated:YES];

    }else if (indexPath.section == 0 & indexPath.row == 2){
        
    SecurityViewController *sVC= [[SecurityViewController alloc] init];
    [self.navigationController pushViewController:sVC animated:YES];

        
    }else if (indexPath.section == 1 & indexPath.row == 0){
     
    CreditNameViewController *creditnameVC= [[CreditNameViewController alloc] init];
    [self.navigationController pushViewController:creditnameVC animated:YES];

    }else if (indexPath.section == 1 & indexPath.row == 1){

    CardNumberBindingViewController *cNBVC= [[CardNumberBindingViewController alloc] init];
    [self.navigationController pushViewController:cNBVC animated:YES];

    }else if (indexPath.section == 2 & indexPath.row == 1){

        if (lz.lzSelected) {
            [lz LZCloseTable];
        } else {
            [lz LZOpenTable];
        }
    }else if (indexPath.section == 4){

    SignViewController *signVC= [[SignViewController alloc] init];
    [self.navigationController pushViewController:signVC animated:YES];
        
    [self obtainData];
    }
    
}
@end
